/*
 * This code based on Silicon Graphics, Inc.'s redbook
 * http://www.opengl.org/resources/code/samples/redbook/
 */

/*
 * Copyright (C) 2008 aki@akjava.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * original code license
 * Copyright (c) 1993-1997, Silicon Graphics, Inc.
 * ALL RIGHTS RESERVED 
 * Permission to use, copy, modify, and distribute this software for 
 * any purpose and without fee is hereby granted, provided that the above
 * copyright notice appear in all copies and that both the copyright notice
 * and this permission notice appear in supporting documentation, and that 
 * the name of Silicon Graphics, Inc. not be used in advertising
 * or publicity pertaining to distribution of the software without specific,
 * written prior permission. 
 *
 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
 * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
 * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
 * 
 * US Government Users Restricted Rights 
 * Use, duplication, or disclosure by the Government is subject to
 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
 * clause at DFARS 252.227-7013 and/or in similar or successor
 * clauses in the FAR or the DOD or NASA FAR Supplement.
 * Unpublished-- rights reserved under the copyright laws of the
 * United States.  Contractor/manufacturer is Silicon Graphics,
 * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
 *
 * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
 */
package com.akjava.android.openglsamples.redbook;

import java.nio.FloatBuffer;
import java.util.HashMap;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;

import com.akjava.android.openglsamples.GLUT;


public class TeapotsRenderer extends RedBookSimpleRenderer {


	public TeapotsRenderer(Context context) {
       super(context);
    }



    public void setUpBuffers() {

    }
    

    public void onSurfaceChanged(GL10 gl, int w, int h) {
    	   gl.glViewport(0, 0,  w,  h);
    	   gl.glMatrixMode(GL10.GL_PROJECTION);
    	   gl.glLoadIdentity();
    	   if (w <= h)
    		   gl.glOrthof(0.0f, 16.0f, 0.0f, 16.0f*(float)h/(float)w,
    	              -10.0f, 10.0f);
    	   else
    		   gl.glOrthof(0.0f, 16.0f*(float)w/(float)h, 0.0f, 16.0f,
    	              -10.0f, 10.0f);
    	   gl.glMatrixMode(GL10.GL_MODELVIEW);
    }



    public void onDrawFrame(GL10 gl) {
    	 gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    	   renderTeapot(gl,2.0f, 17.0f, 0.0215f, 0.1745f, 0.0215f,
    			      0.07568f, 0.61424f, 0.07568f, 0.633f, 0.727811f, 0.633f, 0.6f);
    			   renderTeapot(gl,2.0f, 14.0f, 0.135f, 0.2225f, 0.1575f,
    			      0.54f, 0.89f, 0.63f, 0.316228f, 0.316228f, 0.316228f, 0.1f);
    			   renderTeapot(gl,2.0f, 11.0f, 0.05375f, 0.05f, 0.06625f,
    			      0.18275f, 0.17f, 0.22525f, 0.332741f, 0.328634f, 0.346435f, 0.3f);
    			   renderTeapot(gl,2.0f, 8.0f, 0.25f, 0.20725f, 0.20725f,
    			      1f, 0.829f, 0.829f, 0.296648f, 0.296648f, 0.296648f, 0.088f);
    			   renderTeapot(gl,2.0f, 5.0f, 0.1745f, 0.01175f, 0.01175f,
    			      0.61424f, 0.04136f, 0.04136f, 0.727811f, 0.626959f, 0.626959f, 0.6f);
    			   renderTeapot(gl,2.0f, 2.0f, 0.1f, 0.18725f, 0.1745f,
    			      0.396f, 0.74151f, 0.69102f, 0.297254f, 0.30829f, 0.306678f, 0.1f);
    			   renderTeapot(gl,6.0f, 17.0f, 0.329412f, 0.223529f, 0.027451f,
    			      0.780392f, 0.568627f, 0.113725f, 0.992157f, 0.941176f, 0.807843f,
    			      0.21794872f);
    			   renderTeapot(gl,6.0f, 14.0f, 0.2125f, 0.1275f, 0.054f,
    			      0.714f, 0.4284f, 0.18144f, 0.393548f, 0.271906f, 0.166721f, 0.2f);
    			   renderTeapot(gl,6.0f, 11.0f, 0.25f, 0.25f, 0.25f,
    			      0.4f, 0.4f, 0.4f, 0.774597f, 0.774597f, 0.774597f, 0.6f);
    			   renderTeapot(gl,6.0f, 8.0f, 0.19125f, 0.0735f, 0.0225f,
    			      0.7038f, 0.27048f, 0.0828f, 0.256777f, 0.137622f, 0.086014f, 0.1f);
    			   renderTeapot(gl,6.0f, 5.0f, 0.24725f, 0.1995f, 0.0745f,
    			      0.75164f, 0.60648f, 0.22648f, 0.628281f, 0.555802f, 0.366065f, 0.4f);
    			   renderTeapot(gl,6.0f, 2.0f, 0.19225f, 0.19225f, 0.19225f,
    			      0.50754f, 0.50754f, 0.50754f, 0.508273f, 0.508273f, 0.508273f, 0.4f);
    			   renderTeapot(gl,10.0f, 17.0f, 0.0f, 0.0f, 0.0f, 0.01f, 0.01f, 0.01f,
    			      0.50f, 0.50f, 0.50f, .25f);
    			   renderTeapot(gl,10.0f, 14.0f, 0.0f, 0.1f, 0.06f, 0.0f, 0.50980392f, 0.50980392f,
    			      0.50196078f, 0.50196078f, 0.50196078f, .25f);
    			   renderTeapot(gl,10.0f, 11.0f, 0.0f, 0.0f, 0.0f,
    			      0.1f, 0.35f, 0.1f, 0.45f, 0.55f, 0.45f, .25f);
    			   renderTeapot(gl,10.0f, 8.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f,
    			      0.7f, 0.6f, 0.6f, .25f);
    			   renderTeapot(gl,10.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.55f, 0.55f, 0.55f,
    			      0.70f, 0.70f, 0.70f, .25f);
    			   renderTeapot(gl,10.0f, 2.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.5f, 0.0f,
    			      0.60f, 0.60f, 0.50f, .25f);
    			   renderTeapot(gl,14.0f, 17.0f, 0.02f, 0.02f, 0.02f, 0.01f, 0.01f, 0.01f,
    			      0.4f, 0.4f, 0.4f, .078125f);
    			   renderTeapot(gl,14.0f, 14.0f, 0.0f, 0.05f, 0.05f, 0.4f, 0.5f, 0.5f,
    			      0.04f, 0.7f, 0.7f, .078125f);
    			   renderTeapot(gl,14.0f, 11.0f, 0.0f, 0.05f, 0.0f, 0.4f, 0.5f, 0.4f,
    			      0.04f, 0.7f, 0.04f, .078125f);
    			   renderTeapot(gl,14.0f, 8.0f, 0.05f, 0.0f, 0.0f, 0.5f, 0.4f, 0.4f,
    			      0.7f, 0.04f, 0.04f, .078125f);
    			   renderTeapot(gl,14.0f, 5.0f, 0.05f, 0.05f, 0.05f, 0.5f, 0.5f, 0.5f,
    			      0.7f, 0.7f, 0.7f, .078125f);
    			   renderTeapot(gl,14.0f, 2.0f, 0.05f, 0.05f, 0.0f, 0.5f, 0.5f, 0.4f,
    			      0.7f, 0.7f, 0.04f, .078125f);
    			  gl.glFlush();
    }



	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {setUpBuffers();
		
		   float ambient[] = {0.0f, 0.0f, 0.0f, 1.0f};
		   float diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
		   float specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
		   float position[] = {0.0f, 3.0f, 3.0f, 0.0f};

		   float lmodel_ambient[] = {0.2f, 0.2f, 0.2f, 1.0f};
		   float local_view[] = {0.0f};

		   gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, FloatBuffer.wrap(ambient));
		   gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, FloatBuffer.wrap(diffuse));
		   gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, FloatBuffer.wrap(position));
		   gl.glLightModelfv(GL10.GL_LIGHT_MODEL_AMBIENT, FloatBuffer.wrap(lmodel_ambient));
		   //gl.glLightModelfv(GL10.GL_LIGHT_MODEL_LOCAL_VIEWER, FloatBuffer.wrap(local_view));

		   gl.glFrontFace(GL10.GL_CW);
		   gl.glEnable(GL10.GL_LIGHTING);
		   gl.glEnable(GL10.GL_LIGHT0);
		   //gl.glEnable(GL10.GL_AUTO_NORMAL);
		   gl.glEnable(GL10.GL_NORMALIZE);
		   gl.glEnable(GL10.GL_DEPTH_TEST); 
	}
	
	void renderTeapot(GL10 gl,float x, float y,
			float ambr, float ambg, float ambb,
			float difr, float difg, float difb,
			float specr, float specg, float specb, float shine)
			{
		  float mat[]=new float[4];

			   gl.glPushMatrix();
			   gl.glTranslatef(x, y, 0.0f);
			   mat[0] = ambr; mat[1] = ambg; mat[2] = ambb; mat[3] = 1.0f;
			   gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, getMatBuffer(mat));
			   mat[0] = difr; mat[1] = difg; mat[2] = difb;
			   gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, getMatBuffer(mat));
			   mat[0] = specr; mat[1] = specg; mat[2] = specb;
			   gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR, getMatBuffer(mat));
			   gl.glMaterialf(GL10.GL_FRONT_AND_BACK, GL10.GL_SHININESS, shine * 128.0f);
			   
			   GLUT.glutSolidTorus(gl, 0.5f, 1.0f, 15, 15);
			   
			   gl.glPopMatrix();
			}

	HashMap<String,FloatBuffer> matMap=new HashMap<String,FloatBuffer>();
	
	
public FloatBuffer getMatBuffer(float[] mat){
	String label=""+mat[0]+mat[1]+mat[2]+mat[3];
	FloatBuffer buffer=(FloatBuffer) matMap.get(label);
	if(buffer==null){
		buffer=FloatBuffer.wrap(mat);
		matMap.put(label, buffer);
	}
	return buffer;
}

	


	
}


